layui.use(['element', 'layer', 'form', 'table','laydate','upload'], function () {
    var element = layui.element;
    var table = layui.table;
    var layer = layui.layer;
    var form = layui.form;
    var laydate = layui.laydate;
    var upload = layui.upload;
    $('#cloudDetail').hide();
    $('#cloudAddress').val('');
    $('#cloudDetail').val('');
    var map = new AMap.Map('container', {
        resizeEnable: true,
        zoom: 10
    });
    var trafficLayer = new AMap.TileLayer.Traffic({
        zIndex: 15
    });
    trafficLayer.setMap(map);
    AMapUI.loadUI(['misc/PoiPicker'], function (PoiPicker) {
        var poiPicker = new PoiPicker({
            input: 'cloudAddress'
        });
        poiPickerReady(poiPicker);
    });
    function poiPickerReady(poiPicker) {
        window.poiPicker = poiPicker;
        var marker = new AMap.Marker();
        var infoWindow = new AMap.InfoWindow({
            offset: new AMap.Pixel(0, -20)
        });
        poiPicker.on('poiPicked', function (poiResult) {
            var source = poiResult.source,
                poi = poiResult.item,
                info = {
                    name: poi.name,
                    location: poi.location.toString(),
                    address: poi.district + poi.address
                };
            $('#cloudDetail').val(JSON.stringify(info, null, 2));
            marker.setMap(map);
            infoWindow.setMap(map);
            marker.setPosition(poi.location);
            infoWindow.setPosition(poi.location);
            infoWindow.setContent('基本信息: <pre>' + JSON.stringify(info, null, 2) + '</pre>');
            infoWindow.open(map, marker.getPosition());
        });
        poiPicker.onCityReady(function () {
            poiPicker.suggest('地名地址信息');
        });
    }
    var active = {
        confirmHousebtn:function () {
            var phone = $('#myphone').val();
            var houseName = $('#houseName').val();
            var address = $('#address').val();
            var price = $('#price').val();
            var type = $('input[name="type"]:checked').val();
            var houseType = $('#select_houseType').val();
            var renovation = $('#select_renovation').val();
            var txt = $('#txt').val();
            var waterRate = $('#waterRate').val();
            var electricityFee = $('#electricityFee').val();
            var extralFee = $('#extralFee').val();
            var duiringTime = $('#duiringTime').val();
            var extralData = '';
            var mydata = $('#cloudDetail').val();
            if (mydata == '') {
                layer.msg("未选择云图地点");
                return false;
            }
            $('input[name="extralData"]:checked').each(function(){
                extralData += $(this).val();
                extralData += ','
            });
            if (phone == '' || houseName == '' || address == '' ||
                price == '' || txt == '' || waterRate == '' || electricityFee == '' ||
                extralFee == '' || duiringTime == '' || extralData == '') {
                layer.msg("请先填写完整信息");
                return false;
            }
            var params = $.param({
                cloudInfo: mydata,
                phone:phone,
                duiringTime:duiringTime,
                houseName:houseName,
                address:address,
                price:price,
                type:type,
                houseType:houseType,
                renovation:renovation,
                txt:txt,
                waterRate:waterRate,
                electricityFee:electricityFee,
                extralFee:extralFee,
                extralData:extralData
            }, true);
            $.ajax({
                url:'/front/myCenter/houseInfo/pushNewHouse',
                type:'post',
                data:params,
                dataType:'json',
                success:function (e) {
                    layer.msg(e.msg);
                    if (e.msg == '添加成功') {
                        $('#myuserId').val('');
                        $('#myphone').val('');
                        $('#houseName').val('');
                        $('#address').val('');
                        $('#price').val('');
                        $('[name="type"][value="' + 0 + '"]').prop('checked', true);
                        $('#select_houseType').val(0);
                        $('#select_renovation').val(0);
                        $('#txt').val('');
                        $('#waterRate').val('');
                        $('#electricityFee').val('');
                        $('#extralFee').val('');
                        $('#duiringTime').val('10');
                        $('#cloudAddress').val('');
                        $('#cloudDetail').val('');
                        form.render();
                    }
                }
            });
        }
    };
    $('.confirmHouse .layui-btn').on('click', function () {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });

    upload.render({
        elem:'#batchUpload',
        url:'/front/myCenter/houseInfo/batch/upload',
        accept:'images',
        multiple: true,
        auto: false,
        bindAction:'#confirmUpload',
        choose:function(obj) {
            obj.preview(function(index, file, result){
                $('#showList').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img" style="width: 200px; height: 200px;">&nbsp;&nbsp;&nbsp;&nbsp;')
            });
        },
        allDone: function(obj) {
            layer.msg(obj.successful+"个文件上传成功");
            if (obj.aborted > 0)
                layer.msg(obj.aborted + "个文件上传失败");
        },
        done: function(res){
        }
    })
});